/**
 * describe_instances.java
 * ************************** 
 * @date Jun 1, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package infrastructureAPI.scripts.instanceTools;

import infrastructureAPI.ConcreteRequester;
import infrastructureAPI.ResponseException;
import infrastructureAPI.APIMessages.ResponseMessage;
import infrastructureAPI.algorithms.instanceTools.DescribesInstances;
import infrastructureAPI.backendGate.APIRoutineFunctions;
import infrastructureAPI.backendGate.APIScript;
import infrastructureAPI.backendGate.GeneralScript;
import infrastructureAPI.backendGate.SharedAPIParameters;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Description: 
 *
 */
public class describe_instances extends GeneralScript {

	/**
	 * @param args
	 */
	public describe_instances(String[] args) {
		super(args);
	}

	@Override
	public void checkNotGeneralOptionalParameters() {
		if(arguments.contains(SharedAPIParameters.SUBCLOUD_ID_OPTION)){
			optionalParameterIndex++;
			String subCloudId = arguments.get(
					(arguments.indexOf(SharedAPIParameters.SUBCLOUD_ID_OPTION)+1));
			System.out.println("requested subcloud: "+subCloudId);
		}

	}

	@Override
	public void composeMandatoryParameters() {
		// TODO Auto-generated method stub

	}

	@Override
	public APIScript instantiateAPIScript() {
		InetAddress reqNodeAddress;
		try {
			reqNodeAddress = InetAddress.getLocalHost();
			requester = new ConcreteRequester(reqNodeAddress, registeredServiceName, 
					APIRoutineFunctions.RMIREGISTRY_STADARD_PORT);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		return new DescribesInstances(requester);
	}

	@Override
	public boolean verifyMandatoryParameters() {
		boolean ret = true;
		DescribesInstances describeInstances = new DescribesInstances();
		int param = APIRoutineFunctions.extractParametersNumber(arguments);
		if( param < describeInstances.mandatoryParametersNumber() ){
			System.out.println(describeInstances.showUsage());
			ret = false;
		}
		return ret;
	}

	@Override
	public void waitingIncomingResposes() {
		ResponseMessage resp1 = waitingOneIncomingResponse();
		System.out.println("Requested collected from: "+resp1.getSender());
		try {
			resp1.getAPIResponse().getResponse();
			System.out.println("Waiting for the response:...");
			ResponseMessage resp2 = waitingOneIncomingResponse();
			System.out.println("Reponse:\n"+resp2.getAPIResponse().getResponse());
		} catch (ResponseException e1) {
			e1.printStackTrace();
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		describe_instances decriberScript = new describe_instances(args);
	}

}
